﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BidFunLib.Entity;
using BidFunLib.Services;

namespace BidFunLib.Entity
{
    /// <summary>
    /// 
    /// </summary>
    public class StatsBidSaleLiveUser
    {
        /// <summary>
        /// 
        /// </summary>
        private Double objectRealPrice = 0.0;

        /// <summary>
        /// 
        /// </summary>
        private BidUser bidUser { get; set; }

        /// <summary>
        /// Nb bids done in this sale
        /// </summary>
        public Int32 Nbbids { get; set; }

        /// <summary>
        /// Nb bids manual done in this sale
        /// </summary>
        public Int32 NbBidsManual { get; set; }

        /// <summary>
        /// Nb bids by bot done in this sale
        /// </summary>
        public Int32 NbBidsByBot { get; set; }

        /// <summary>
        /// Pondered bid in this sale
        /// This we tell us if user bid at the beginning or at the end of the sale
        /// </summary>
        public Int32 PonderedNbBids { get; set; }

        /// <summary>
        /// Psychologic ratio = 100 * (nbbids * 0.75 + bidAmount) / object.realPrice
        /// </summary>
        public Double PsychologicRatio { get; set; }

        /// <summary>
        /// Price that player will paid if he win the bid
        /// </summary>
        public Double PriceToPaidIfWinner { get; set; }

        /// <summary>
        /// Le prix déja payé pour le joueur (valuer du point * nbBids)
        /// </summary>
        public Double PriceAlreadyPaid { get; set; }

        /// <summary>
        /// La durée en seconds entre la dernière enchère et maintenant.
        /// </summary>
        public DateTime NoBidDurationTime { get; set; }

        /// <summary>
        /// Date et heure de la dernière enchère de ce joueur
        /// </summary>
        public DateTime LastBidDateTime { get; set; }

        /// <summary>
        /// Static data only loaded in constructor
        /// </summary>
        public StatsBidUserByBidObject statsBidUserByBidObject { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public BidOperatorTypeManager.BID_OPERATOR_TYPE OperatorType { get; set; }


        /// <summary>
        /// 
        /// </summary>
        public void updateNoBidDurationTime()
        {
          //  NoBidDurationTime = DateTime.Now - LastBidDateTime;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="?"></param>
        /// <param name="?"></param>
        public void updateStatsBidSaleLiveWithBidLogger(BidSale bidSale, BidLogger bidLogger)
        {
            Nbbids++;
            OperatorType = bidLogger.BidOperatorType.BidOperatorTypeAsEnum;

            if (bidLogger.BidOperatorType.BidOperatorTypeAsEnum == BidFunLib.Services.BidOperatorTypeManager.BID_OPERATOR_TYPE.AUTOMATIC ||
                bidLogger.BidOperatorType.BidOperatorTypeAsEnum == BidFunLib.Services.BidOperatorTypeManager.BID_OPERATOR_TYPE.NONE)
            {
                NbBidsByBot++;
            }
            else if (bidLogger.BidOperatorType.BidOperatorTypeAsEnum == BidFunLib.Services.BidOperatorTypeManager.BID_OPERATOR_TYPE.MANUAL)
            {
                NbBidsManual++;
            }

            PonderedNbBids += (int)(bidLogger.AmountAsDouble * 100);

            Double bidAmount = bidLogger.AmountAsDouble;

            PriceAlreadyPaid = 0.75 * Nbbids;

            PriceToPaidIfWinner = PriceAlreadyPaid + bidAmount;

            PsychologicRatio = 100 * ((PriceAlreadyPaid + bidAmount) / objectRealPrice);

            LastBidDateTime = bidLogger.DateTime;
        }

        /// <summary>
        /// update les statistiques dynamiques de la bidSale pour un nouveau et un seul BidLogger
        /// </summary>
        /// <param name="bidSale"></param>
        public StatsBidSaleLiveUser(BidSale bidSale, BidLogger bidLogger)
        {
            // store the bid user
            bidUser = bidLogger.BidUser;

            objectRealPrice = bidSale.bidObject.RealPriceAsDouble;

            // we must retreive statistics of the users for this object
            statsBidUserByBidObject = StatsBidUserByBidObjectManager.Instance.getStatsBidUserByBidObjectByBidUser(bidLogger.BidUser, bidSale.BidObject);
        }
    }
}
